package sketch.ui;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import sketch.doc.Point;
import sketch.doc.SketchContainer;
import sketch.doc.Stroke;

/**
 *
 * @author  zhengwt
 */
public class SketchSampler extends JFrame {

	public static final int SEG_NUM = 20;
	public static final int PARSE_SYMBOL_NUM = 4;
	public static final int PARSE_LINK_NUM = 4;
	public static final int PARSE_ALL_NUM = 3;

    public SketchSampler() {
        initComponents();
    }

    private void initComponents() {
    	getButtonGroup();
    	getButtonGroupSub();

    	//TODO: initialization
    	getJRadioButtonSeg().setSelected(true);
    	getJRadioButtonSymbol().setSelected(true);
    	getJTextFieldName().setText("UnknownUser");
    	setSubEnabled(false);
    	setComboBoxID(SEG_NUM);
    	updateFileName();

    	setResizable(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(getJPanelCommand(), javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(getJPanelSketch(), javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(getJPanelCommand(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(getJPanelSketch(), javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();

//        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(getSize().width, getSize().height + 100);
        setLocation(100, 30);
    }

    public static void main(String args[]) {
    	try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SketchSampler().setVisible(true);
            }
        });
    }

    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.ButtonGroup buttonGroupSub;
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonDir;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JComboBox jComboBoxID;
    private javax.swing.JLabel jLabelDir;
    private javax.swing.JLabel jLabelFile;
    private javax.swing.JLabel jLabelID;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelSubType;
    private javax.swing.JLabel jLabelType;
    private javax.swing.JPanel jPanelCommand;
    private javax.swing.JPanel jPanelSketch;
    private javax.swing.JRadioButton jRadioButtonAll;
    private javax.swing.JRadioButton jRadioButtonLink;
    private javax.swing.JRadioButton jRadioButtonParse;
    private javax.swing.JRadioButton jRadioButtonSeg;
    private javax.swing.JRadioButton jRadioButtonSymbol;
    private javax.swing.JTextField jTextFieldDir;
    private javax.swing.JTextField jTextFieldFile;
    private javax.swing.JTextField jTextFieldName;

	public javax.swing.ButtonGroup getButtonGroup() {
		if (buttonGroup == null) {
			buttonGroup = new ButtonGroup();
			buttonGroup.add(getJRadioButtonSeg());
			buttonGroup.add(getJRadioButtonParse());
		}
		return buttonGroup;
	}

	public javax.swing.ButtonGroup getButtonGroupSub() {
		if (buttonGroupSub == null) {
			buttonGroupSub = new ButtonGroup();
			buttonGroupSub.add(getJRadioButtonSymbol());
	        buttonGroupSub.add(getJRadioButtonLink());
	        buttonGroupSub.add(getJRadioButtonAll());
		}
		return buttonGroupSub;
	}

	public javax.swing.JButton getJButtonClear() {
		if (jButtonClear == null) {
			jButtonClear = new JButton();
	        jButtonClear.setText("Clear");

	        jButtonClear.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					SketchContainer.get().clear();
					repaint();
				}

	        });
		}
		return jButtonClear;
	}

	public javax.swing.JButton getJButtonDir() {
		if (jButtonDir == null) {
			jButtonDir = new JButton();
			jButtonDir.setText("...");

			jButtonDir.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					JFileChooser ch = new JFileChooser(".");
					ch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					ch.showOpenDialog(SketchSampler.this);
					File dir = ch.getSelectedFile();
					if (dir != null)
						getJTextFieldDir().setText(dir.getAbsolutePath());
				}

			});
		}
		return jButtonDir;
	}

	public javax.swing.JButton getJButtonSave() {
		if (jButtonSave == null) {
			jButtonSave = new JButton();
			jButtonSave.setText("Save");

			jButtonSave.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					File file = new File(getFileName());
//					System.out.println(file.getAbsolutePath());
					SketchContainer.get().save(file);
				}

			});
		}
		return jButtonSave;
	}

	private String getFileName() {
		return getJTextFieldDir().getText()
		+ "\\" + getJTextFieldFile().getText();
	}

	public javax.swing.JComboBox getJComboBoxID() {
		if (jComboBoxID == null) {
			jComboBoxID = new JComboBox();
			jComboBoxID.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					updateFileName();
				}

			});
		}
		return jComboBoxID;
	}

	public void setComboBoxID(int num) {
		String[] ss = new String[num];
		for (int i = 1; i < ss.length+1; i++) {
			ss[i-1] = Integer.toString(i);
		}
		getJComboBoxID().setModel(new DefaultComboBoxModel(ss));
	}

	public javax.swing.JLabel getJLabelDir() {
		if (jLabelDir == null) {
			jLabelDir = new JLabel();
			jLabelDir.setText("Save directory:");
		}
		return jLabelDir;
	}

	public javax.swing.JLabel getJLabelFile() {
		if (jLabelFile == null) {
			jLabelFile = new JLabel();
			jLabelFile.setText("Sample file name:");
		}
		return jLabelFile;
	}

	public javax.swing.JLabel getJLabelID() {
		if (jLabelID == null) {
			jLabelID = new JLabel();
			jLabelID.setText("Sample ID:");
		}
		return jLabelID;
	}

	public javax.swing.JLabel getJLabelName() {
		if (jLabelName == null) {
			jLabelName = new JLabel();
			jLabelName.setText("Your name:");
		}
		return jLabelName;
	}

	public javax.swing.JLabel getJLabelSubType() {
		if (jLabelSubType == null) {
			jLabelSubType = new JLabel();
			jLabelSubType.setText("Sub type for parsing:");
		}
		return jLabelSubType;
	}

	public javax.swing.JLabel getJLabelType() {
		if (jLabelType == null) {
			jLabelType = new JLabel();
			jLabelType.setText("Sample type:");
		}
		return jLabelType;
	}

	public javax.swing.JPanel getJPanelCommand() {
		if (jPanelCommand == null) {
			jPanelCommand = new JPanel();
			jPanelCommand.setBorder(
					BorderFactory.createTitledBorder(
							BorderFactory.createLineBorder(
									new java.awt.Color(0, 0, 0)), "Command"));

			javax.swing.GroupLayout jPanelCommandLayout = new javax.swing.GroupLayout(jPanelCommand);
	        jPanelCommand.setLayout(jPanelCommandLayout);
	        jPanelCommandLayout.setHorizontalGroup(
	            jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(getJLabelSubType())
	                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                        .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                                .addComponent(getJLabelName())
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(getJTextFieldName(), javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
	                            .addComponent(getJLabelType())
	                            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                                .addGap(10, 10, 10)
	                                .addComponent(getJRadioButtonSymbol())
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(getJRadioButtonLink())
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(getJRadioButtonAll()))
	                            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                                .addGap(10, 10, 10)
	                                .addComponent(getJRadioButtonSeg())
	                                .addGap(6, 6, 6)
	                                .addComponent(getJRadioButtonParse())))
	                        .addGap(55, 55, 55)
	                        .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addComponent(getJLabelID(), javax.swing.GroupLayout.Alignment.TRAILING)
	                                    .addComponent(getJLabelDir(), javax.swing.GroupLayout.Alignment.TRAILING)
	                                    .addComponent(getJLabelFile(), javax.swing.GroupLayout.Alignment.TRAILING))
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                                    .addComponent(getJComboBoxID(), javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                    .addComponent(getJTextFieldFile(), javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                    .addGroup(jPanelCommandLayout.createSequentialGroup()
	                                        .addComponent(getJTextFieldDir(), javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
	                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                        .addComponent(getJButtonDir()))))
	                            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                                .addComponent(getJButtonSave())
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                                .addComponent(getJButtonClear())))))
	                .addContainerGap(40, Short.MAX_VALUE))
	        );
	        jPanelCommandLayout.setVerticalGroup(
	            jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanelCommandLayout.createSequentialGroup()
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(getJLabelName())
	                    .addComponent(getJTextFieldName(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(getJLabelDir())
	                    .addComponent(getJTextFieldDir(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(getJButtonDir()))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(getJLabelType())
	                    .addComponent(getJLabelID())
	                    .addComponent(getJComboBoxID(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(getJRadioButtonSeg())
	                    .addComponent(getJRadioButtonParse())
	                    .addComponent(getJLabelFile())
	                    .addComponent(getJTextFieldFile(), javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addComponent(getJLabelSubType())
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(jPanelCommandLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(getJRadioButtonSymbol())
	                    .addComponent(getJRadioButtonLink())
	                    .addComponent(getJRadioButtonAll())
	                    .addComponent(getJButtonSave())
	                    .addComponent(getJButtonClear()))
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
		}
		return jPanelCommand;
	}

	public javax.swing.JPanel getJPanelSketch() {
		if (jPanelSketch == null) {
			jPanelSketch = new JPanel()  {
				public void paint(Graphics arg0) {
					super.paint(arg0);
					Graphics2D g2 = (Graphics2D) arg0;

					SketchContainer.get().draw(g2);
				}
			};

			jPanelSketch.setBorder(
					BorderFactory.createTitledBorder(
							BorderFactory.createLineBorder(
									new java.awt.Color(0, 0, 0)), "Sketch Area"));

			jPanelSketch.addMouseMotionListener(new MouseMotionListener() {

				public void mouseDragged(MouseEvent e) {
					SketchContainer.get().appendPoint(new Point(e.getX(), e.getY()));
					SketchContainer.get().drawCurrent((Graphics2D)getJPanelSketch().getGraphics());
				}

				public void mouseMoved(MouseEvent e) {
					// TODO Auto-generated method stub

				}
			});

			jPanelSketch.addMouseListener(new MouseAdapter() {

				@Override
				public void mousePressed(MouseEvent e) {
					super.mousePressed(e);
					SketchContainer.get().appendPoint(new Point(e.getX(), e.getY()));
				}

				@Override
				public void mouseReleased(MouseEvent e) {
					super.mouseReleased(e);

					SketchContainer.get().appendPoint(new Point(e.getX(), e.getY()));
					Stroke s = SketchContainer.get().fetchStroke();
					SketchContainer.get().open().addStroke(s);

					repaint();
				}

			});

			javax.swing.GroupLayout jPanelSketchLayout = new javax.swing.GroupLayout(jPanelSketch);
	        jPanelSketch.setLayout(jPanelSketchLayout);
	        jPanelSketchLayout.setHorizontalGroup(
	            jPanelSketchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 669, Short.MAX_VALUE)
	        );
	        jPanelSketchLayout.setVerticalGroup(
	            jPanelSketchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGap(0, 382, Short.MAX_VALUE)
	        );


		}

		jPanelSketch.setBackground(Color.WHITE);
		return jPanelSketch;
	}

	public javax.swing.JRadioButton getJRadioButtonAll() {
		if (jRadioButtonAll == null) {
			jRadioButtonAll = new JRadioButton();
			jRadioButtonAll.setText("All");
	        jRadioButtonAll.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
	        jRadioButtonAll.setMargin(new java.awt.Insets(0, 0, 0, 0));

	        jRadioButtonAll.addActionListener(al);
		}
		return jRadioButtonAll;
	}

	public javax.swing.JRadioButton getJRadioButtonLink() {
		if (jRadioButtonLink == null) {
			jRadioButtonLink = new JRadioButton();
			jRadioButtonLink.setText("Link");
	        jRadioButtonLink.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
	        jRadioButtonLink.setMargin(new java.awt.Insets(0, 0, 0, 0));

	        jRadioButtonLink.addActionListener(al);
		}
		return jRadioButtonLink;
	}

	public javax.swing.JRadioButton getJRadioButtonParse() {
		if (jRadioButtonParse == null) {
			jRadioButtonParse = new JRadioButton();
			jRadioButtonParse.setText("parsing");
	        jRadioButtonParse.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
	        jRadioButtonParse.setMargin(new java.awt.Insets(0, 0, 0, 0));
	        jRadioButtonParse.addActionListener(al);
	        jRadioButtonParse.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					if (getJRadioButtonParse().isSelected()) {
						setSubEnabled(true);
					}
				}

			});
		}
		return jRadioButtonParse;
	}

	private void setSubEnabled(boolean b) {
		getJRadioButtonSymbol().setEnabled(b);
		getJRadioButtonAll().setEnabled(b);
		getJRadioButtonLink().setEnabled(b);
	}

	private void updateFileName() {
		StringBuffer fn = new StringBuffer();
		String n = getJTextFieldName().getText();
		n = n.replaceAll(" ", "");
		n = n.replaceAll("\t", "");
		fn.append(n);

		if (getJRadioButtonSeg().isSelected()) {
			fn.append(".seg");
		} else {
			fn.append(".parse");

			if (getJRadioButtonSymbol().isSelected())
				fn.append(".symbol");
			else if (getJRadioButtonLink().isSelected())
				fn.append(".link");
			else
				fn.append(".all");
		}

		fn.append(".");
		fn.append(getJComboBoxID().getSelectedIndex() + 1);

		getJTextFieldFile().setText(fn.toString());
	}

	private void updateComboBox() {
		if (getJRadioButtonParse().isSelected()) {
			if (getJRadioButtonSymbol().isSelected()) {
				setComboBoxID(PARSE_SYMBOL_NUM);
			} else if (getJRadioButtonLink().isSelected()) {
				setComboBoxID(PARSE_LINK_NUM);
			} else {
				setComboBoxID(PARSE_ALL_NUM);
			}
		} else {
			setComboBoxID(SEG_NUM);
		}
	}

	private ActionListener al = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			updateComboBox();
			updateFileName();
		}

	};

	public javax.swing.JRadioButton getJRadioButtonSeg() {
		if (jRadioButtonSeg == null) {
			jRadioButtonSeg = new JRadioButton();
			jRadioButtonSeg.setText("segmentation");
	        jRadioButtonSeg.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
	        jRadioButtonSeg.setMargin(new java.awt.Insets(0, 0, 0, 0));
	        jRadioButtonSeg.addActionListener(al);
	        jRadioButtonSeg.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					if (getJRadioButtonSeg().isSelected()) {
						setSubEnabled(false);
					}
				}

	        });
		}
		return jRadioButtonSeg;
	}

	public javax.swing.JRadioButton getJRadioButtonSymbol() {
		if (jRadioButtonSymbol == null) {
			jRadioButtonSymbol = new JRadioButton();
			jRadioButtonSymbol.setText("Symbol");
	        jRadioButtonSymbol.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
	        jRadioButtonSymbol.setMargin(new java.awt.Insets(0, 0, 0, 0));

	        jRadioButtonSymbol.addActionListener(al);
		}
		return jRadioButtonSymbol;
	}

	public javax.swing.JTextField getJTextFieldDir() {
		if (jTextFieldDir == null) {
			jTextFieldDir = new JTextField();
			jTextFieldDir.setEditable(false);
			jTextFieldDir.setText(new File("").getAbsolutePath());
		}
		return jTextFieldDir;
	}

	public javax.swing.JTextField getJTextFieldFile() {
		if (jTextFieldFile == null) {
			jTextFieldFile = new JTextField();
			jTextFieldFile.setEditable(false);
		}
		return jTextFieldFile;
	}

	public javax.swing.JTextField getJTextFieldName() {
		if (jTextFieldName == null) {
			jTextFieldName = new JFormattedTextField();

			jTextFieldName.getDocument().addDocumentListener(new DocumentListener() {

				public void changedUpdate(DocumentEvent e) {
					updateFileName();
				}

				public void insertUpdate(DocumentEvent e) {
					updateFileName();
				}

				public void removeUpdate(DocumentEvent e) {
					updateFileName();
				}

			});

		}
		return jTextFieldName;
	}



}
